package com.zhouyu;


import com.zhouyu.service.PrimaryBean;
import com.zhouyu.service.UserService;
import com.zhouyu.seven.OrderServiceSeven;
import com.zhouyu.seven.OrderServiceSeven3;
import com.zhouyu.seven.QualifierBean;
import com.zhouyu.tool.StringToOrderServiceSevenConverter;
import com.zhouyu.tool.StringToUserPropertyEditor;
import com.zhouyu.service.User;
import com.zhouyu.tool.StringToUserConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.ResourceBundleMessageSource;

import javax.annotation.Priority;
import java.beans.PropertyEditor;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@ComponentScan(value = "com.zhouyu")
@Configuration
@PropertySources({ @PropertySource("classpath:spring.properties"),
		@PropertySource("classpath:spring1.properties")})
public class AppConfig {


	@Bean
	@Qualifier("a")
	public QualifierBean qualifierBean(){
		QualifierBean qualifierBean = new QualifierBean();
		System.out.println("bean a:" + qualifierBean);
		return qualifierBean;
	}

	@Bean
	@Qualifier("b")
	public QualifierBean qualifierBean1(){
		QualifierBean qualifierBean = new QualifierBean();
		System.out.println("bean b:" + qualifierBean);
		return qualifierBean;
	}

	@Bean(autowireCandidate = false)
	public OrderServiceSeven3 orderServiceSeven3(){
		return  new OrderServiceSeven3();
	}


	@Bean
	public OrderServiceSeven3 orderServiceSeven34(){
		return new OrderServiceSeven3();
	}


//	@Bean("xiaozhou")
//	public OrderServiceSeven orderServiceSeven(){
//		return new OrderServiceSeven();
//	}

	@Bean
	public String zhouyu(){
		return "springEL表达式";
	}


	@Bean
	public ConversionServiceFactoryBean conversionService() {
		ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
		conversionServiceFactoryBean.setConverters(Collections.singleton(new StringToUserConverter()));
		return conversionServiceFactoryBean;
	}

	@Bean
	public CustomEditorConfigurer customEditorConfigurer() {
		CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
		Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();

		// 表示StringToUserPropertyEditor可以将String转化成User类型，在Spring源码中，
		// 如果发现当前对象是String，而需要的类型是User，就会使用该PropertyEditor来做类型转化
		propertyEditorMap.put(User.class, StringToUserPropertyEditor.class);
		customEditorConfigurer.setCustomEditors(propertyEditorMap);
		return customEditorConfigurer;
	}

	@Bean
	public ApplicationListener applicationListener() {
		return new ApplicationListener() {
			@Override
			public void onApplicationEvent(ApplicationEvent event) {
//				System.out.print("输出发布的结果:");
//				System.out.println(((PayloadApplicationEvent) event).getPayload());

			}
		};
	}

	@Bean
	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename("messages");
		return messageSource;
	}

//	@Bean
//	public JdbcTemplate jdbcTemplate() {
//		return new JdbcTemplate(dataSource());
//	}
//
//	@Bean
//	public PlatformTransactionManager transactionManager() {
//		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
//		transactionManager.setDataSource(dataSource());
//		return transactionManager;
//	}
//
//	@Bean
//	public DataSource dataSource() {
//		DriverManagerDataSource dataSource = new DriverManagerDataSource();
//		dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/tuling?characterEncoding=utf-8&amp;useSSL=false");
//		dataSource.setUsername("root");
//		dataSource.setPassword("Zhouyu123456***");
//		return dataSource;
//	}
//

//	@Bean
//	public MapperScannerConfigurer configurer(){
//		MapperScannerConfigurer configurer = new MapperScannerConfigurer();
//		configurer.setBasePackage("com.luban.dao");
//
//		return configurer;
//	}
//


//	@Bean
//	public SqlSessionFactory sqlSessionFactory() {
//		SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
//		sessionFactoryBean.setDataSource(dataSource());
//		try {
//			return sessionFactoryBean.getObject();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		return null;
//	}


}
